home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / terminal.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-14  |  16.0 KB  |  716 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- // 
  5. // C++ Source Code File Name: terminal.cpp
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: Doug Gaer    
  8. // File Creation Date: 03/21/1997 
  9. // Date Last Modified: 03/15/1999
  10. // Copyright (c) 1997 Douglas M. Gaer
  11. // ----------------------------------------------------------- // 
  12. // ------------- Program Description and Details ------------- // 
  13. // ----------------------------------------------------------- // 
  14. /*
  15. The VBD C++ classes are copyright (c) 1997, by Douglas M. Gaer.
  16. All those who put this code or its derivatives in a commercial
  17. product MUST mention this copyright in their documentation for
  18. users of the products in which this code or its derivative
  19. classes are used. Otherwise, you have the freedom to redistribute
  20. verbatim copies of this source code, adapt it to your specific
  21. needs, or improve the code and release your improvements to the
  22. public provided that the modified files carry prominent notices
  23. stating that you changed the files and the date of any change.
  24.  
  25. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
  26. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
  27. IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
  28. YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
  29. CORRECTION.
  30.  
  31. This is a terminal interface designed to be portable between
  32. DOS and UNIX systems. On UNIX systems the "curses" library is
  33. used to create terminal independent code. On MSDOS/Windows95
  34. systems the ANSI.SYS driver is used to simulate the basic
  35. functions of the "curses" library.
  36. */
  37. // ----------------------------------------------------------- // 
  38. #include <ctype.h>
  39. #include <string.h>
  40. #include "terminal.h"
  41.  
  42. #ifdef __DOS__
  43. #include <iostream.h>
  44. #include <iomanip.h>
  45. #endif
  46.  
  47. #ifdef __UNIX__
  48. #include <sys/time.h>
  49. #include <termio.h>
  50. #endif
  51.  
  52. Terminal I_TERM;              // Independent Teminal type object
  53. Terminal *terminal = &I_TERM; // Global terminal pointer
  54.  
  55. void Terminal::GetString (char *string, int x, int y)
  56. // This function allows the user to enter a single string.
  57. // It was added to echo each character without having to
  58. // to turn the echo on. This will allow the uesr to edit
  59. // the entry using the backspace or other defined keys.
  60. {
  61.   char ch;
  62.   int charcount = 0;
  63.   
  64.   if (x != -1) move (y, x);
  65.  
  66.   while (1) {
  67.     ch = getch();
  68.     if (ch == '\n' || ch == '\r' || ch == CONTROL('c')) {
  69.       *string = 0;
  70.       return;
  71.     }
  72.  
  73.     if (ch == '\b' || ch == CONTROL('d')) { // Delete keys 
  74.       if (charcount > 0) {
  75.     string--;
  76.     charcount--;
  77.     Write('\b');
  78.     Write(' ');
  79.     Write('\b');
  80.       }
  81.     }
  82.     else {
  83.       *string++ = ch;
  84.       charcount++;
  85.       Write(ch);
  86.     }
  87.     refresh();
  88.   }
  89. }
  90.  
  91. void Terminal::GetPassword (char *string, int x, int y)
  92. // This function allows the user to enter a password
  93. // and echo an asterisk for each character typed.
  94. {
  95.   char ch;
  96.   int charcount = 0;
  97.   
  98.   if (x != -1) move (y, x);
  99.  
  100.   while (1) {
  101.     ch = getch();
  102.     if (ch == '\n' || ch == '\r' || ch == CONTROL('c')) {
  103.       *string = 0;
  104.       return;
  105.     }
  106.  
  107.     if (ch == '\b' || ch == CONTROL('d')) { // Delete keys 
  108.       if (charcount > 0) {
  109.     string--;
  110.     charcount--;
  111.     Write('\b');
  112.     Write(' ');
  113.     Write('\b');
  114.       }
  115.     }
  116.     else {
  117.       *string++ = ch;
  118.       charcount++;
  119.       Write('*');
  120.     }
  121.     refresh();
  122.   }
  123. }
  124.  
  125. int Terminal::GetInt(int x, int y)
  126. // Get a signed int value.
  127. {
  128.   char buf[100];
  129.   GetString(buf, x, y);
  130.   return atoi(buf);
  131. }
  132.  
  133. long Terminal::GetLong(int x, int y)
  134. // Get a signed long value.
  135. {
  136.   char buf[100];
  137.   GetString(buf, x, y);
  138.   return atol(buf);
  139. }
  140.  
  141. double Terminal::GetFloat(int x, int y)
  142. // Get a floating point value
  143. {
  144.   char buf[100];
  145.   GetString(buf, x, y);
  146.   return atof(buf);
  147. }
  148.  
  149. int Terminal::YesNo(int x, int y)
  150. {
  151.   if(x == -1) 
  152.     Write(" <Do you wish to continue (y/n)> "); // Same line message
  153.   else
  154.     Write("Do you wish to continue (y/n)...", x, y);
  155.  
  156.   char c = ' ';
  157.   while (c != 'y' && c != 'n')    {
  158.     c = GetChar();
  159.     c = tolower(c);
  160.   }
  161.   return c == 'y' ? 1: 0; // return true if users answers yes
  162. }
  163.  
  164. int Terminal::YesNo(const char *s, int x, int y)
  165. {
  166.   if(x == -1) 
  167.     Write(s); // Same line message
  168.   else
  169.     Write(s, x, y);
  170.  
  171.   char c = ' ';
  172.   while (c != 'y' && c != 'n')    {
  173.     c = GetChar();
  174.     c = tolower(c);
  175.   }
  176.   return c == 'y' ? 1: 0; // return true if users answers yes
  177. }
  178.  
  179. int Terminal::GetYesNo()
  180. // Wait for y/n reply with no prompt.
  181. {
  182.   char c = ' ';
  183.   while (c != 'y' && c != 'n')    {
  184.     c = GetChar();
  185.     c = tolower(c);
  186.   }
  187.   return c == 'y' ? 1: 0; // return true if users answers yes
  188. }
  189.  
  190. void Terminal::PutBack(char c)
  191. // Put back a keyboard character
  192. {
  193.   putback = c;
  194. }
  195.  
  196. int Terminal::Center(const char *s) const
  197. // Returns the center coordinate for a string.
  198. {
  199.   int len = strlen(s);
  200.   if(len > MaxCols()-1) len = MaxCols()-1;
  201.   int mid = MaxCols()/2 - 1;
  202.   int half = len/2;
  203.   return mid - half;
  204. }
  205.  
  206. int Terminal::Center(char *s)
  207. // Returns the center coordinate for a string.
  208. {
  209.   int len = strlen(s);
  210.   if(len > MaxCols()-1) len = MaxCols()-1;
  211.   int mid = MaxCols()/2 - 1;
  212.   int half = len/2;
  213.   return mid - half;
  214. }
  215.  
  216. int Terminal::ScreenCenter(const int offset) const
  217. // Returns the center coordinate for the screen.
  218. {
  219.   int buf = offset;
  220.   if(buf > MaxLines()-1) buf = MaxLines()-1;
  221.   int mid = MaxLines()/2 - 1;
  222.   int half = buf/2;
  223.   return mid - half;
  224. }
  225.  
  226. int Terminal::ScreenCenter(int offset)
  227. // Returns the center coordinate for the screen.
  228. {
  229.   if(offset > MaxLines()-1) offset = MaxLines()-1;
  230.   int mid = MaxLines()/2 - 1;
  231.   int half = offset/2;
  232.   return mid - half;
  233. }
  234.  
  235. int Terminal::Right(const char *s) const
  236. // Returns the right justifed coordinate for a string.
  237. {
  238.   int len = strlen(s);
  239.   if(len > MaxCols()-1) len = MaxCols()-1;
  240.   int end = MaxCols()-1;
  241.   return end - len;
  242. }
  243.  
  244. int Terminal::Right(char *s)
  245. // Returns the right justifed coordinate for a string.
  246. {
  247.   int len = strlen(s);
  248.   if(len > MaxCols()-1) len = MaxCols()-1;
  249.   int end = MaxCols()-1;
  250.   return end - len;
  251. }
  252.  
  253. void Terminal::StatusLine(const char *s) const
  254. // Display a status line in normal text mode.
  255. {
  256.   Write(s, 0, MaxLines()-1);
  257.   int len = strlen(s);
  258.   while (len++ < MaxCols()-1) Write(' ');
  259. }
  260.  
  261. void Terminal::StatusLine(char *s) 
  262. // Display a status line in normal text mode.
  263. {
  264.   Write(s, 0, MaxLines()-1);
  265.   int len = strlen(s);
  266.   while (len++ < MaxCols()-1) Write(' ');
  267. }
  268.  
  269. void Terminal::ClearLine(int x, int y) const
  270. {
  271.   int i = 0;
  272.   if(x == -1) 
  273.     while (i++ < MaxCols()-1) Write(' ');
  274.   else {
  275.     MoveCursor(x, y);
  276.     while (i++ < MaxCols()-1) Write(' ');
  277.   }
  278. }
  279.  
  280. void Terminal::ClearLine(int x, int y)
  281. {
  282.   int i = 0;
  283.   if(x == -1) 
  284.     while (i++ < MaxCols()-1) Write(' ');
  285.   else {
  286.     MoveCursor(x, y);
  287.     while (i++ < MaxCols()-1) Write(' ');
  288.   }
  289. }
  290.  
  291. void Terminal::MoveCursor(int x, int y) const
  292. {
  293.   move(y, x);
  294.   refresh();
  295. }
  296.  
  297. void Terminal::MoveCursor(int x, int y)
  298. {
  299.   move(y, x);
  300.   refresh();
  301. }
  302.  
  303. void Terminal::ClearScreen() const
  304. {
  305.   clear();
  306. }
  307.  
  308. void Terminal::ClearScreen() 
  309. {
  310.   clear();
  311. }
  312.  
  313. void Terminal::AnyKey(int x, int y) 
  314. // Prompt the user for any key to continue
  315. {
  316.   if(x == -1) {
  317.     Write(" <Press any key to continue> "); // Same line message
  318.     GetChar();
  319.     return;
  320.   }
  321.   Write("Press any key to continue...", x, y);
  322.   GetChar();
  323. }
  324.  
  325. void Terminal::AnyKey(const char *s, int x, int y) 
  326. // Prompt the user for any key to continue
  327. {
  328.   if(x == -1) {
  329.     Write(s); // Same line message
  330.     GetChar();
  331.     return;
  332.   }
  333.   Write(s, x, y);
  334.   GetChar();
  335. }
  336.  
  337. void Terminal::Write(const unsigned char c, int x, int y) const
  338. // Write a unsigned character to the screen.
  339. {
  340.   if (x != -1) move (y, x);
  341.   char s[1];
  342.   s[0] = c;    // Copy char into a string buffer
  343.   s[1] = '\0'; // Null terminate the string
  344.   addstr(s);
  345.   refresh();
  346. }
  347.  
  348. void Terminal::Write(unsigned char c, int x, int y) 
  349. // Write a unsigned character to the screen.
  350. {
  351.   if (x != -1) move (y, x);
  352.   char s[1];
  353.   s[0] = c;    // Copy char into a string buffer
  354.   s[1] = '\0'; // Null terminate the string
  355.   addstr(s);
  356.   refresh();
  357. }
  358.  
  359. void Terminal::Write(const char c, int x, int y) const
  360. // Write a signed character to the screen.
  361. {
  362.   if (x != -1) move (y, x);
  363.   char s[1];
  364.   s[0] = c;    // Copy char into a string buffer
  365.   s[1] = '\0'; // Null terminate the string
  366.   addstr(s);
  367.   refresh();
  368. }
  369.  
  370. void Terminal::Write(char c, int x, int y) 
  371. // Write a signed character to the screen.
  372. {
  373.   if (x != -1) move (y, x);
  374.   char s[1];
  375.   s[0] = c;    // Copy char into a string buffer
  376.   s[1] = '\0'; // Null terminate the string
  377.   addstr(s);
  378.   refresh();
  379. }
  380.  
  381. void Terminal::Write(const char *s, int x, int y) const
  382. // Write a single string to the screen. 
  383. {
  384.   if (x != -1) move (y, x);
  385.   addstr(s);
  386.   refresh();
  387. }
  388.  
  389. void Terminal::Write(char *s, int x, int y) 
  390. // Write a single string to the screen. 
  391. {
  392.   if (x != -1) move (y, x);
  393.   addstr(s);
  394.   refresh();
  395. }
  396.  
  397. void Terminal::Write(const long val, int x, int y) const
  398. // Write a signed long int to the screen.
  399. {
  400.   char buf[100];
  401.   sprintf(buf, "%d", val);
  402.   Write(buf, x, y);
  403. }
  404.  
  405. void Terminal::Write(long val, int x, int y)
  406. // Write a signed long int to the screen.
  407. {
  408.   char buf[100];
  409.   sprintf(buf, "%d", val);
  410.   Write(buf, x, y);
  411. }
  412.  
  413. void Terminal::Write(const int val, int x, int y) const
  414. // Write a signed int to the screen.
  415. {
  416.   char buf[100];
  417.   sprintf(buf, "%d", val);
  418.   Write(buf, x, y);
  419. }
  420.  
  421. void Terminal::Write(int val, int x, int y)
  422. // Write a signed int to the screen.
  423. {
  424.   char buf[100];
  425.   sprintf(buf, "%d", val);
  426.   Write(buf, x, y);
  427. }
  428.  
  429. void Terminal::Write(const double val, int x, int y) const 
  430. // Write a floating point value to the screen.
  431. {
  432.   char buf[100];
  433.   sprintf(buf, "%.2f", val);
  434.   Write(buf, x, y);
  435. }
  436.  
  437. void Terminal::Write(double val, int x, int y) 
  438. // Write a floating point value to the screen.
  439. {
  440.   char buf[100];
  441.   sprintf(buf, "%.2f", val);
  442.   Write(buf, x, y);
  443. }
  444.  
  445. void Terminal::Write(const float val, int x, int y) const 
  446. // Write a floating point value to the screen.
  447. {
  448.   char buf[100];
  449.   sprintf(buf, "%.2f", val);
  450.   Write(buf, x, y);
  451. }
  452.  
  453. void Terminal::Write(float val, int x, int y) 
  454. // Write a floating point value to the screen.
  455. {
  456.   char buf[100];
  457.   sprintf(buf, "%.2f", val);
  458.   Write(buf, x, y);
  459. }
  460.  
  461. void Terminal::StandOut(const char *s, int x, int y) const
  462. // Write text in stand out mode.
  463. {
  464.   if (x != -1) move (y, x);
  465.   standout();
  466.   addstr(s);
  467.   standend();
  468.   refresh();
  469. }
  470.  
  471. void Terminal::StandOut(char *s, int x, int y) 
  472. // Write text in stand out mode.
  473. {
  474.   if (x != -1) move (y, x);
  475.   standout();
  476.   addstr(s);
  477.   standend();
  478.   refresh();
  479. }
  480.  
  481. unsigned char Terminal::GetChar() 
  482. // Input a keyboard charater (unbuffered) with out waiting
  483. // for the RETURN key.
  484. {
  485.   int c;
  486.   if(putback) {
  487.     c = putback;
  488.     putback = 0;
  489.   }
  490.   else {
  491.     c = getch();
  492.     if (c == 0) // Convert function key
  493.       c = getch() | 0x80;
  494.     else // Strip scan code
  495.       c &= 0xff;
  496.   }
  497.   return (unsigned char)(c);
  498. }
  499.  
  500. int Terminal::KBWait() const
  501. // Test for a keyboard character waiting. Continue processing until
  502. // the user interrupts by hitting a key.
  503. {
  504. #ifdef __DOS__
  505.   return kbhit();
  506. #endif
  507.  
  508. #ifdef __UNIX__
  509.   fd_set readfds, writefds, exceptfds;
  510.   struct timeval timeout;
  511.   static struct termio otty, ntty;
  512.   int ret;
  513.  
  514.   // Create proper environment for select() 
  515.   FD_ZERO( &readfds );
  516.   FD_ZERO( &writefds );
  517.   FD_ZERO( &exceptfds );
  518.   FD_SET( fileno(stdin), &readfds );
  519.  
  520.   // We shall specify 0.5 sec as the waiting time 
  521.   timeout.tv_sec  = 0;     // 0 seconds 
  522.   timeout.tv_usec = 500; // 500 microseconds 
  523.  
  524.   int fd = 0;
  525.   
  526.   // Put tty in raw mode 
  527.   ioctl(fd, TCGETA, &otty);
  528.   ntty = otty;
  529.   ntty.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL);
  530.   ntty.c_lflag &= ~ICANON;
  531.   ntty.c_lflag |= ISIG;
  532.   ntty.c_cflag &= ~(CSIZE|PARENB);
  533.   ntty.c_cflag |= CS8;
  534.   ntty.c_iflag &= (ICRNL|ISTRIP);
  535.   ntty.c_cc[VMIN] = 1;
  536.   ntty.c_cc[VTIME] = 1;
  537.   ioctl(fd, TCSETAW, &ntty);
  538.  
  539.   // Do a select 
  540.   ret = select( 1, &readfds, &writefds, &exceptfds, &timeout );
  541.  
  542.   // Reset the tty back to its original mode 
  543.   ioctl(fd, TCSETAW, &otty);
  544.   
  545.   return( ret );
  546. #endif
  547. }
  548.  
  549. void Terminal::init()
  550. {
  551. #ifdef __UNIX__
  552.  
  553. // Removed: causes core dump if term varible is set incorrectly (ie: dec-vt320)
  554. // newterm(getenv("TERM"), stdout, stdin);
  555.  
  556.   initscr();            // initialize the curses library 
  557.   keypad(stdscr, TRUE); // enable keyboard mapping 
  558.   idlok(stdscr, TRUE);  // enable terminal insert and delete line features 
  559.   nonl();               // tell curses not to do NL->CR/NL on output 
  560.   noecho();             // don't echo input 
  561.   raw();                // Allows the program to mask Ctrl-C
  562.  
  563.   // Curses input modes
  564.   // cbreak();   // Send all characters to program except Ctrl-C,S,Q, and Z
  565.   // nocbreak(); // Nothing is sent to the program until the users hits return
  566.   // raw();      // All charaters are sent immediately to the program 
  567.   // noraw();    // Turn off raw mode
  568.  
  569.   maxcols = COLS;
  570.   maxlines = LINES;
  571.  
  572. #endif
  573.  
  574. #ifdef __DOS__
  575.   // Simulate an initscr() call 
  576.   cout << "\033[m"; // Normal text attributes
  577.   cout.flush();
  578.   clear();
  579.   maxcols = COLS;
  580.   maxlines = LINES;
  581.   move(0, 0);
  582.   refresh();
  583. #endif
  584. }
  585.  
  586. void Terminal::finish()
  587. {
  588.   clear();
  589.   endwin();
  590. }
  591.  
  592. void Terminal::SetMaxLines(int lines)
  593. {
  594.   if(lines > LINES)
  595.     maxlines = LINES;
  596.   else
  597.     maxlines = lines;
  598. }
  599.  
  600. void Terminal::SetMaxCols(int cols)
  601. {
  602.   if(cols > COLS)
  603.     maxcols = COLS;
  604.   else
  605.     maxcols = cols;
  606. }
  607.  
  608. #ifdef __DOS__
  609. // ===============================================================
  610. // Stand alone functions used to port this code from UNIX to DOS,
  611. // Windows 3.11, and Windows 95. These functions are used to
  612. // simulate the basic functionality of the vast CURSES library.
  613. // All of these functions rely on the ANSI.SYS driver installed
  614. // in the "config.sys" file: DEVICEHIGH=C:\WINDOWS\COMMAND\ANSI.SYS
  615. // in Windows 95 or DEVICEHIGH=C:\DOS\ANSI.SYS in DOS/Windows 3.11.
  616. // ===============================================================
  617. int LINES = 25; // PC terminal the maximum screen height 
  618. int COLS  = 80; // PC terminal the maximum screen width
  619.  
  620. int addch(const chtype c)
  621. {
  622.   cout << (unsigned char)c;
  623.   cout.flush();
  624.   return OK;
  625. }
  626.  
  627. int addstr(const char *s)
  628. {
  629.   if(s) cout << s;
  630.   cout.flush();
  631.   return OK;
  632. }
  633.  
  634. int beep()
  635. {
  636.   cout << '\a';
  637.   return OK;
  638. }
  639.  
  640. int clear()
  641. {
  642.   cout << "\033[2J";
  643.   cout.flush();
  644.   return OK;
  645. }
  646.  
  647. int endwin()
  648. {
  649.   cout << "\033[m"; // Normal text attributes
  650.   cout.flush();
  651.   clear();
  652.   move(0, 0);
  653.   return OK;
  654. }
  655.  
  656. int move(int y, int x)
  657. {
  658.   cout << "\033[" << (y+1) << ';' << (x+1) << 'H';
  659.   cout.flush();
  660.   return OK;
  661. }
  662.  
  663. int refresh()
  664. {
  665.   return OK;
  666. }
  667.  
  668. int standend()
  669. {
  670.   cout << "\033[m"; // Normal text attributes
  671.   cout.flush();
  672.   return OK;
  673. }
  674.  
  675. int standout()
  676. {
  677.   cout << "\033[7m"; // Reverse video text attribute
  678.   cout.flush();
  679.   return OK;
  680. }
  681. // ===============================================================
  682. // The folling code is used for PC enhancements, used to emulate
  683. // the effect of the video attribute functions in the curses library.
  684. // All of the escape codes are compatible with vt100 terminals.
  685.  
  686. void UnderlineText()
  687. {
  688.   cout << "\033[4m"; 
  689.   cout.flush();
  690. }
  691.  
  692. void BoldText()
  693. {
  694.   cout << "\033[1m"; 
  695.   cout.flush();
  696. }
  697.  
  698. void ReverseVideo()
  699. {
  700.   cout << "\033[7m"; 
  701.   cout.flush();
  702. }
  703.  
  704. void NormalText()
  705. {
  706.   cout << "\033[m"; // Normal text attributes
  707.   cout.flush();
  708. }
  709. // ===============================================================
  710. #endif // __DOS__
  711.  
  712. // ----------------------------------------------------------- //
  713. // ------------------------------- //
  714. // --------- End of File --------- //
  715. // ------------------------------- //
  716.